package sprint1;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * @author Vincent van Andel
 * @author Fabian Mengerink
 *
 */
public class Character {

	private Scanner in = new Scanner(System.in);
	private ArrayList<Location> locations = new ArrayList<Location>();
	private ArrayList<Item> items = new ArrayList<Item>();
	private Location current;
	private int currentLocation;
	private Inventory inventory = new Inventory(12);
	private ArrayList<Item> inventoryItems = new ArrayList<Item>();
	private double hpMax;
	private double hpNow;
	private double armour;
	private double power;
	private int dicesize;
	private int speed;
	// private String hpString = hpBalkmax.substring(0,(int)(50 * hpNowME / hpMaxME)) +
	// hpBalkmin.substring(0,(50-(int)(50* hpNowME / hpMaxME)));
	private double critChance;
	private double critDamage;
	private boolean opgepakt = false;

	public Character(double hpMax, double armour, double power, int dicesize,
			int speed, double critChance, double critDamage) {
		this.hpMax = hpMax;
		this.hpNow = hpMax;
		this.armour = armour;
		this.power = power;
		this.dicesize = dicesize;
		this.speed = speed;
		this.critChance = critChance;
		this.critDamage = critDamage;
		locations = Location.addLocation();
		items = Item.addItem();
	}

	public double getHpMax() {
		return this.hpMax;
	}

	public void setHpMax(double hpMax) {
		this.hpMax = hpMax;
	}

	public double getHpNow() {
		return this.hpNow;
	}

	public void setHpNow(double hpNow) {
		this.hpNow = hpNow;
	}

	public double getArmour() {
		return this.armour;
	}

	public void setArmour(double armour) {
		this.armour = armour;
	}

	public double getPower() {
		return this.power;
	}

	public void setPower(double power) {
		this.power = power;
	}

	public int getDicesize() {
		return this.dicesize;
	}

	public void setDicesize(int dicesize) {
		this.dicesize = dicesize;
	}

	public int getSpeed() {
		return this.speed;
	}

	public void setSpeed(int speed) {
		this.speed = speed;
	}

	public double getCritChance() {
		return this.critChance;
	}

	public void setCritChance(double critChance) {
		this.critChance = critChance;
	}

	public double getCritDamage() {
		return this.critDamage;
	}

	public void setCritDamage(double critDamage) {
		this.critDamage = critDamage;
	}

	public void setCurrent(Location current) {
		this.current = current;
	}

	public void setInventory(Inventory inventory) {
		this.inventory = inventory;
	}

	public ArrayList<Location> getLocations() {
		return this.locations;
	}

	public ArrayList<Item> getItems() {
		return this.items;
	}

	public Location getCurrent() {
		return this.current;
	}

	public int getCurrentLocation() {
		return this.currentLocation;
	}

	public Inventory getInventory() {
		return this.inventory;
	}

	public ArrayList<Item> getInventoryItems() {
		return this.inventoryItems;
	}

	public void chooseLocation(int nr) {
		setCurrentLocation(nr);
		Location location = getLocation(nr);
		locations = location.ArrayList(nr);
		System.out.println("Naar welke kamer wil je gaan:");
		if (location instanceof Teleport) {
			int teleportnr = ((Teleport) location).getKeynr();
			int keynr = 0;
			inventoryItems = inventory.getInventory();
			for (Item i : inventoryItems) {
				if (i instanceof Key) {
					keynr = ((Key) i).getUniqueNumber();
				}
			}
			if (keynr == teleportnr) {
				for (Location l : locations) {
					System.out.println(l);
				}
			} else { // als je niet de benodigde key hebt
				location.getCon1();
			}
		} else { // dan is het dus automatisch een kamer
			for (Location l : locations) {
				System.out.println(l);
			}
		}
		int chooseChamber = in.nextInt();
		setCurrentLocation(chooseChamber);
	}

	private Location getLocation(int nr) {
		ArrayList<Location> locations = Location.addLocation();
		for (Location l : locations) {
			if (l.getNr() == nr) {
				return l;
			}
		}
		return null;
	}

	public void setCurrentLocation(int currentLocation) {
		this.currentLocation = currentLocation;
		current = getLocation(currentLocation);
		int monster = current.getMonster();
		if (monster > 0 && monster < 4) {
			// TODO: make randoms
		}
		if (monster > 3) {
			Alien alien = null;
			ArrayList<Alien> aliens = Boot.getAliens();
			for (int i = 0; i < aliens.size(); i++) {
				if (monster == (i + 3)) {
					alien = aliens.get(i);
					BattleSystem bs = new BattleSystem(alien, Boot
							.getCharacter());
					bs.simuleer(Boot.getCharacter());
				}
			}
		}

		if (opgepakt && currentLocation == 7) {
			String[] filename = (current.getFileName()).split(".txt");
			String strFilename = filename[0].toString() + "01.txt";
			Location.printOut(strFilename);
		} else {
			String filename = current.getFileName();
			Location.printOut(filename);
		}
	}

	public void choice() {
		int choice = 0;
		System.out.println("Wat wil je doen?");
		System.out.println("1 = met een object ");
		System.out.println("2 = naar een andere kamer gaan");
		choice = in.nextInt();
		if (choice == 1) {
			useObject();
		} else if (choice == 2) {
			chooseLocation(currentLocation);
			choice();
		} else {
			System.out.println("Geen geldige keus");
			choice();
		}
	}

	private void useObject() {
		System.out.println("Met welk object wil je wat gaan doen?");
		Item item = null;
		for (int i = 0; i < items.size(); i++) {
			if (current.getObject() == (i + 1)) {
				item = items.get(i);
				System.out.println(item);
				System.out.println("Wil je het oppakken? Toets dan 1 in");
				int choice = in.nextInt();
				if (choice == 1) {
					if (opgepakt == false) {
						inventory.addItem(item);
						opgepakt = true;
						if (item.getName().startsWith("key")) {
							String[] filename = (current.getFileName())
									.split(".txt");
							String strFilename = filename[0].toString()
									+ "01.txt";
							Location.printOut(strFilename);
						}
					}
				}
			}
		}
		choice();
	}
}
